<?php
class Apps_UserinfoController extends Mediad2_Controller_Apps_Action
{	
	public function indexAction()
	{
		//获取user表的信息
		$serviceUser = new Mediad2_Service_Admin_User();
		$re = $serviceUser->find($this->uid);
		if($re->getCode() == $re::CODE_SUCCESS) {
			$model    = $re->getData();
			$email    = $model->getEmail();
			$nickname = $model->getNickname();
			$mobile   = $model->getMobile();
			$qq 	  = $model->getQq();
			$pwd      = $model->getPwd();
			$this->pageview->assign("email",$email);
			$this->pageview->assign("nickname",$nickname);
			$this->pageview->assign("mobile",$mobile);
			$this->pageview->assign("qq",$qq);
			$this->pageview->assign("password",$pwd);
		}
		//获取userbank表的信息
		$serviceUserBank = new Mediad2_Service_Admin_GloUserBank();
		$where = "uid = $this->uid";
		$result = $serviceUserBank->findBy($where);
		
		if($result->getCode() == $result::CODE_SUCCESS){
			$bankModel   = $result->getData();
			$account 	 = $bankModel->getAccount();
			$bankdeposit = $bankModel->getBankdeposit();
			$name        = $bankModel->getName();
			$type 		 = $bankModel->getType();
			switch($type){
				case "1":
					$type = "银行转账";
				break;
				case "2":
					$type = "支付宝转账";
				break;
				case "3":
					$type = "财付通转账";
				break;		
			}
			$this->pageview->assign("bank_type",$type);
			$this->pageview->assign("bank_account",$account);
			$this->pageview->assign("bank_name",$name);
			$this->pageview->assign("bank_bankdeposit",$bankdeposit);
		}
		
		$this->helper->setPageView($this->pageview, WEB_TITLE, "userinfo", null); 
	}
	
	/* 修改用户基本信息 */
	public function updateuserinfoAction()
	{
		if ($this->uid<1) {
			echo $this->encodeResult($this->returnResult(Mediad2_Model_Result::CODE_FAILURE,"Missing parameter:uid..."));
		} else {
			//获取user信息
			$service = new Mediad2_Service_Admin_User();
			$result = $service->find($this->uid);
			if ($result->getCode() == Mediad2_Model_Result::CODE_FAILURE) {
				echo $this->encodeResult($this->result);
			} 
			$user = $result->getData();
			$user->setNickname($this->httpdata['nickname']);
			$user->setEmail($this->httpdata['email']);
			$user->setQq($this->httpdata['qq']);
			$user->setMobile($this->httpdata['mobile']);
			$result = $service->update($user);
			echo $this->encodeResult($result);
		}
	}
	
	/* 修改密码 */
	public function resetpwdAction(){
		header("Content-type:text/html;charset=utf-8");
		if($this->getRequest()->isPost ()){
		
			$arr['result'] = 0;
			$pwd = md5($_POST['pwd']); 
			$requestData['pwd'] = $pwd;
			$requestData['isadmin']    		= $this->isadmin;
			$requestData['status']     		= "1";
			$requestData['addtime']    		= $this->addtime;
			$requestData['rolecode']   		= "";
			$requestData['verifycode'] 		= "";
			$requestData['cid'] 	   		= $this->cid;
			$requestData['uid']        		= $this->uid;
			$requestData['email']      		= $this->email;
			$requestData['mobile']     		= $this->mobile;
			$requestData['nickname']   		= $this->nickname;
			$requestData['qq']    	   		= $this->qq;
			$requestData['channelstatus']   = $this->channelstatus;
			
			$objUser  = new Mediad2_Service_Admin_User();
			$objM 	  = new Mediad2_Service_Util_M($objUser);
			$helper = new Mediad2_Utility_SmartyHelper();
			$helper->log("out page");
			if($objM->create($requestData)){
				$helper->log("in page");
				$result = $objM->save();
				$rel = $result->getCode();
				$arr['result'] = $rel;
			}
			echo json_encode($arr);
		}
	}
	/*验证输入的旧密码是否一致 */
	public function verifypwdAction(){
		if($this->getRequest()->isPost ()){
			$data = $this->getRequest ()->getPost ();
			$oldPass = $data['oldPass'];
			$oldPass = md5($oldPass);
			$title = $data['fail'];
			if($oldPass == $this->pwd){
				$title = $data['success'];
			}
			echo json_encode($title);
		}
	}
	
	/*修改成功的过渡页*/
	public function updatesuccessAction(){
		$registr_title = "修改成功";
		$time 		   = "5";
		$url 		   = "login";
		$content 	   = "修改成功，5秒后自动跳转到登陆页...";
		$link_content  = "点击这里直接返回到登陆页";	
		$this->pageview->assign("Retitle",$registr_title);
		$this->pageview->assign("Retime",$time);
		$this->pageview->assign("content",$content);
		$this->pageview->assign("Reurl",$url);
		$this->pageview->assign("link_content",$link_content);
		$this->helper->setPageView($this->pageview, WEB_TITLE, "success", null);
	}
	
}